package com.hollycrm.springframework.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class StringUtils
    extends org.apache.commons.lang.StringUtils {

    public static List stringToList(String str, String split) {
        List r = new ArrayList();
        if ( (str != null) && (!str.trim().equals(""))) {
            str = leftTrim(str, split);
            int pre = 0;
            int index = str.indexOf(split, pre);
            while (index >= 0) {
                String freg = str.substring(pre, index);
                if (freg != null && !freg.trim().equals("")) {
                    r.add(freg);
                }
                pre = index + split.length();
                index = str.indexOf(split, pre);
            }
            String left = str.substring(pre);
            if (left != null && !left.trim().equals("")) {
                r.add(left);
            }
        }
        return r;
    }

    private static String trim(String s1, String s2) {
        s1 = leftTrim(s1, s2);
        return rightTrim(s1, s2);
    }

    private static String leftTrim(String s1, String s2) {
        while (s1.startsWith(s2)) {
            s1 = s1.substring(s2.length());
        }
        return s1;
    }

    private static String rightTrim(String s1, String s2) {
        while (s1.endsWith(s2)) {
            s1 = s1.substring(0, s1.length() - s2.length());
        }
        return s1;
    }

    public static String listToString(List list, String link) {
        StringBuffer sb = new StringBuffer();
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            sb.append(iterator.next()).append(link);
        }
        String rt = sb.toString();
        if (rt.length() > link.length()) {
            rt = rt.substring(0, rt.length() - link.length());
        }
        return rt;
    }

}
